import Vue from 'vue'

Vue.mixin({
	data () {
		return {

		}
	},
	mounted () {

	},
	methods: {
		
		// 格式成千分位
		$commafy (val) {
			let num = JSON.stringify(val)
			num = num + "";
			if (/^.*\..*$/.test(num)) {
				var pointIndex = num.lastIndexOf(".");
				var intPart = num.substring(0, pointIndex);
				var pointPart = num.substring(pointIndex + 1, num.length);
				intPart = intPart + "";
				var re = /(-?\d+)(\d{3})/
				while (re.test(intPart)) {
					intPart = intPart.replace(re, "$1,$2")
				}
				num = intPart + "." + pointPart;
			} else {
				num = num + "";
				var re = /(-?\d+)(\d{3})/
				while (re.test(num)) {
					num = num.replace(re, "$1,$2")
				}
			}
			return num;
		},
		// 格式化时间搓
		$formatDate (datetime, type) {
			if (!datetime) return ''
			let date = new Date(datetime); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
			let year = date.getFullYear(),
				month = ("0" + (date.getMonth() + 1)).slice(-2),
				sdate = ("0" + date.getDate()).slice(-2),
				hour = ("0" + date.getHours()).slice(-2),
				minute = ("0" + date.getMinutes()).slice(-2),
				second = ("0" + date.getSeconds()).slice(-2);
			let result = ''

			if (!type) {
				result = year + "-" + month + "-" + sdate + " " + hour + ":" + minute + ":" + second;
			}
			if (type && type === 'date') {
				result = year + "-" + month + "-" + sdate;
			}
			return result;
		},
		// 防抖
		$UseDebounce (fn, delay) {
			if (this.Timeout) clearTimeout(this.Timeout)
			this.Timeout = setTimeout(() => {
				if (typeof fn === "function") fn()
			}, delay)
		},
		// 显示 loading
		$showLoading (title) {
			uni.showLoading({
				title: '加载中...'
			})
		},
		// 隐藏 loading
		$hideLoading (delay = 0) {
			setTimeout(() => {
				uni.hideLoading();
			}, delay)
		},
		// 弹窗提示 delay 是否延迟
		$showToast (msg, duration = 2000) {
			uni.showToast({
				icon: 'none',
				title: msg,
				duration: duration
			});
		},
		// 成功弹窗提示
		$toastSuccess (msg, duration = 2000) {
			uni.showToast({
				icon: 'success',
				title: msg,
				duration: duration
			});
		},
		// 跳转页面
		$navigateTo (url, delay = 0) {
			if (!url || url == '') return
			setTimeout(() => {
				uni.navigateTo({
					url: url
				});
			}, delay)
		},
		// 跳转tabBar页面
		$switchTab (url, delay = 0) {
			if (!url || url == '') return
			setTimeout(() => {
				uni.switchTab({
					url: url
				});
			}, delay)
		},
		// 返回页面 返回上一页面或多级页面 number控制返回层级
		$navigateBack (number = 1, delay = 0) {
			setTimeout(() => {
				uni.navigateBack({
					delta: number
				});
			}, delay)
		},
		// 关闭当前页面, 跳转页面
		$redirectTo (url, delay = 0) {
			if (!url || url == '') return
			setTimeout(() => {
				uni.redirectTo({
					url: url
				});
			}, delay)
		},
		// 设置缓存
		$setStorage (name, value) {
			return uni.setStorageSync(name, value);
		},
		// 获取缓存
		$getStorageSync (name) {
			return uni.getStorageSync(name);
		},
		// 清除缓存
		$removeStorage (name) {
			return uni.removeStorage({
				key: name
			});
		},
		// 清除全部缓存
		$ClearStorage () {
			return uni.clearStorageSync();
		},
		//修改头部title内容
		$setTopTitle (content) {
			uni.setNavigationBarTitle({
				title: content,
			})
		},
		$getRequest () {
			var url = window.location.search; //获取url中"?"符后的字串
			var theRequest = new Object();
			if (url.indexOf("?") != -1) {
				var str = url.substr(1);
				var strs = str.split("&");
				for (var i = 0; i < strs.length; i++) {
					theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
				}
			}
			return theRequest;
		},
	}
})
